package com.sharemob.tinchat.base;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sharemob.tinchat.R;

public class ListAdapter extends BaseAdapter {


	ArrayList<Item> list = new ArrayList<Item>();

	@Override
	public int getCount()
	{
		return list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return list.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder viewHolder = null;
		if (convertView == null)
		{
			viewHolder = new ViewHolder();

			convertView = View.inflate(parent.getContext(),R.layout.sub_listview_item, null);
			findViews(viewHolder, convertView);
			
			convertView.setTag(viewHolder);
		}
		else
		{
			
			viewHolder = (ViewHolder) convertView.getTag();
		}
		setView(viewHolder, position);
		return convertView;
	}

	private void setView(final ViewHolder viewHolder, int position)
	{
		Item item = list.get(position);
		if(item.isIcon()||item.getResid()!=0){
			viewHolder.itemIcon.setBackgroundResource(item.getResid());
			viewHolder.itemIcon.setVisibility(ImageView.VISIBLE);
		}
		
		if(item.isIv_selected()){
			viewHolder.item_iv_selected.setVisibility(View.VISIBLE);
		}else{
			viewHolder.item_iv_selected.setVisibility(View.GONE);
		}
		
		if(item.getMidIcon()!=null){
			viewHolder.itemMidIcon.setVisibility(View.VISIBLE);
			viewHolder.itemMidIcon.setImageBitmap(item.getMidIcon());
		}
		
		if(item.isArrow_icon()){
			viewHolder.item_arrow_icon.setVisibility(View.VISIBLE);
		}else{
			viewHolder.item_arrow_icon.setVisibility(View.GONE);
		}
		
		if(item.isSwitchBtn()){
			viewHolder.item_switchBtn.setVisibility(View.VISIBLE);
			viewHolder.item_switchBtn.setSlideListener(item.listener);
		}else{
			viewHolder.item_switchBtn.setVisibility(View.GONE);
			viewHolder.item_switchBtn.setSlideListener(null);
		}
		
		if(null!=item.getRightName()&&!item.getRightName().equals("")){
			viewHolder.itemRightName.setVisibility(View.VISIBLE);
			viewHolder.itemRightName.setText(item.getRightName());
		}else{
			viewHolder.itemRightName.setVisibility(View.GONE);
		}
		
		if(item.getName()==null){
			viewHolder.itemName.setVisibility(View.GONE);
		}else{
			viewHolder.itemName.setVisibility(View.VISIBLE);
			viewHolder.itemName.setText(item.getName());
		}
		
		if(item.getDrawLeft()!=0){
			viewHolder.item_tv_drawleft.setVisibility(View.VISIBLE);
			viewHolder.item_tv_drawleft.setImageResource(item.getDrawLeft());
		}else{
			viewHolder.item_tv_drawleft.setVisibility(View.GONE);
		}
	}

	public void setAdapter(ArrayList<Item> list)
	{
		this.list = list;
	}

	private void findViews(ViewHolder viewHolder, View convertView)
	{
		viewHolder.itemIcon = (ImageView) convertView.findViewById(R.id.listview_item_icon);
		viewHolder.itemName = (TextView) convertView.findViewById(R.id.listview_item_title);
		viewHolder.item_arrow_icon = (ImageView) convertView.findViewById(R.id.listview_item_arrow);
		viewHolder.itemRightName=(TextView)convertView.findViewById(R.id.listview_item_right_title);
		viewHolder.itemMidIcon=(CircleImageView) convertView.findViewById(R.id.listview_item_midIcon);
		viewHolder.item_switchBtn=(SlideSwitch) convertView.findViewById(R.id.switch_btn);
		viewHolder.item_tv_drawleft=(ImageView)convertView.findViewById(R.id.listview_item_tvdrawleft);
		viewHolder.item_iv_selected=(ImageView)convertView.findViewById(R.id.listview_item_iv_selected);
	}

	protected final class ViewHolder
	{
		ImageView itemIcon;
		TextView itemName;
		TextView itemRightName;
		ImageView item_arrow_icon;
		ImageView item_iv_selected;
		ImageView item_tv_drawleft;
		CircleImageView itemMidIcon;
		SlideSwitch item_switchBtn;
		
	}

}
